package io.xuan.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.xuan.modules.app.dao.OrderDao;
import io.xuan.modules.app.entity.CarInfoEntity;
import io.xuan.modules.app.entity.OrderEntity;
import io.xuan.modules.app.service.CarInfoService;
import io.xuan.modules.app.service.OrderService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("orderServiceImpl")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity>
        implements OrderService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    private AsyncServer asyncServer;

    @Autowired
    private CarInfoService carInfoService;


    @Override
    public List<OrderEntity> queryByUserId(Long userId) {
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderEntity::getUid, userId);
        return this.list(queryWrapper);
    }

//    @Autowired
//    private OrderDao orderDao;

    @Override
    public List<OrderEntity> queryByDriveId(Long userId) {
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderEntity::getDriveId, userId);
        return this.list(queryWrapper);
    }

    @Override
    public List<OrderEntity> allOrder() {
        return this.list();
    }

    @Override
    public boolean updateOrder(Integer hasEvaluate, String orderId) {
        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setHasEvaluate(hasEvaluate);
        LambdaQueryWrapper<OrderEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderEntity::getOrderId, orderId);
        return this.update(orderEntity, queryWrapper);
    }


    @Test
    public void test() {
        // 假设 uid 和 driveId 已经有值了
        int uid = 123;
        int driveId = 456;


        LocalDateTime currentDateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String dateTimeString = currentDateTime.format(formatter);
        // 生成当前日期时间的字符串
        // 创建唯一的 ID
        String uniqueId = dateTimeString + "-" + uid + "-" + driveId;

        System.out.println("Generated ID: " + uniqueId);
    }


    // 模拟订单事件推送
    @Test
    public void simulateOrderEvents() {
        ArrayList<String> orderEvents = new ArrayList<>();
        int uid = 123;
        int driveId = 110;
        Double peopleLat = 123.232323;
        Double peopleLng = 34.342231;
        orderEvents.add(String.valueOf(uid));
        orderEvents.add(String.valueOf(driveId));
        orderEvents.add(String.valueOf(peopleLat));
        orderEvents.add(String.valueOf(peopleLng));
        System.out.println(Arrays.toString(orderEvents.toArray()));
        //redisTemplate.opsForList().leftPush("order_queue", orderEvents.toString());
    }


    @Override
    public boolean saveOrder(int uid, int driveId, Double peopleLat, Double peopleLng) {
        CarInfoEntity carInfo = carInfoService.getCarInfo(driveId);
        Double carLat = carInfo.getCarLat();
        Double carLng = carInfo.getCarLng();
        double distance = calculateDistance(carLat, carLng, peopleLat, peopleLng);
        if (distance > 50) {
            System.out.println("超出距离了哥");
            return false;
        } else {
            OrderEntity orderEntity = new OrderEntity();
            orderEntity.setDriveId(driveId);
            orderEntity.setUid(uid);
            LocalDateTime currentDateTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
            String dateTimeString = currentDateTime.format(formatter);
            // 生成当前日期时间的字符串
            // 创建唯一的 ID
            String uniqueId = dateTimeString + "-" + uid + "-" + driveId;
            System.out.println(uniqueId);
            orderEntity.setOrderId(uniqueId);
            //orderEntity.setId("test1");
            System.out.println(orderEntity.getOrderId());
            orderEntity.setBusId(carInfo.getCarId());
            orderEntity.setHasEvaluate(0);
            orderEntity.setAmount(1);
            orderEntity.setPayMethod("微信");
            orderEntity.setCurrentStation(peopleLat + " " + peopleLng);
            return this.save(orderEntity);
        }
        // 其他逻辑
    }


    private double calculateDistance(Double lat1, Double lon1, Double lat2, Double lon2) {
        // 根据经纬度计算两点之间的距离，这里只是示例，实际使用时应使用更精确的计算方法
        double earthRadius = 6371000; // 地球半径，单位米
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = earthRadius * c;
        return distance;
    }
}
